﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DADOS.esf;
using DADOS.usuario;
using System.Data;

namespace PROAD.admin.Relatorio
{
    public partial class RelatorioSintetico : System.Web.UI.Page
    {
        DADOS.usuario.metPaciente _metPac = new metPaciente();
        DADOS.esf.metEsf _metEsf = new metEsf();
        DADOS.auxiliares.metEndereco metAux = new DADOS.auxiliares.metEndereco();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                
                CarregarDdl();
            }
            
        }

        private void CarregarDdl()
        {
            ddlUf.DataSource = metAux.ListaUF();
            ddlUf.DataBind();
            ddlUf.Items.Insert(0, new ListItem("Selecione uma UF", "0"));


        }
        protected void ddlUf_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlMunicipio.DataSource = metAux.ListaCidade(Convert.ToInt32(ddlUf.SelectedValue));
            ddlMunicipio.DataBind();
            ddlMunicipio.Items.Insert(0, new ListItem("Selecione um Município", "0"));
        }
        protected void PreencherTela()
        {

            PnlConsulta.Visible = false;
            pnlResult.Visible = true;
            int cidade = 0;
            if (!string.IsNullOrEmpty(ddlMunicipio.SelectedValue)) cidade = Convert.ToInt32(ddlMunicipio.SelectedValue);
            var objUsuario = _metPac.ListarPacientesPorLocalidade(Convert.ToInt32(ddlUf.SelectedValue), cidade);
            var objEquipe = _metEsf.ListarEquipes(Convert.ToInt32(ddlUf.SelectedValue), cidade);

            int count = 0;
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
            int count4 = 0;

            int countEq = 0;
            int countEq1 = 0;
            int countEq2 = 0;
            int countEq3 = 0;
            int countEq4 = 0;

            foreach (DataRow item in objEquipe.Tables[0].Rows)
            {
                if (item["CO_ATENCAO_SUS"].ToString() == "1")
                {
                    countEq1++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "2")
                {
                    countEq2++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "3")
                {
                    countEq3++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "4")
                {
                    countEq4++;
                }
                countEq++;
            }

            foreach (DataRow item in objUsuario.Tables[0].Rows)
            {
                if (item["CO_ATENCAO_SUS"].ToString() == "1")
                {
                    count1++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "2")
                {
                    count2++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "3")
                {
                    count3++;
                }
                if (item["CO_ATENCAO_SUS"].ToString() == "4")
                {
                    count4++;
                }
                count++;
            }

            lblTotalUser.Text = count.ToString();
            lblTotalUser1.Text = count1.ToString();
            lblTotalUser2.Text = count2.ToString();
            lblTotalUser3.Text = count3.ToString();
            lblTotalUser4.Text = count4.ToString();

            lblTotalEquip.Text = countEq.ToString();
            lblTotalEquip1.Text = countEq1.ToString();
            lblTotalEquip2.Text = countEq2.ToString();
            lblTotalEquip3.Text = countEq3.ToString();
            lblTotalEquip4.Text = countEq4.ToString();

            decimal result = 0;
            decimal result1 = 0;
            decimal result2 = 0;
            decimal result3 = 0;
            decimal result4 = 0;

            try
            {
                 result = Convert.ToDecimal(lblTotalUser.Text) / Convert.ToDecimal(lblTotalEquip.Text);
            }
            catch (Exception)
            {
                result = 0;
            }

            try
            {
                result1 = Convert.ToDecimal(lblTotalUser1.Text) / Convert.ToDecimal(lblTotalEquip1.Text);
            }
            catch (Exception)
            {
                result1 = 0;
            }

            try
            {
                result2 = Convert.ToDecimal(lblTotalUser2.Text) / Convert.ToDecimal(lblTotalEquip2.Text);
            }
            catch (Exception)
            {
                result2 = 0;
            }

            try
            {
                result3 = Convert.ToDecimal(lblTotalUser3.Text) / Convert.ToDecimal(lblTotalEquip3.Text);
            }
            catch (Exception)
            {
                result3 = 0;
            }

            try
            {
                result4 = Convert.ToDecimal(lblTotalUser4.Text) / Convert.ToDecimal(lblTotalEquip4.Text);
            }
            catch (Exception)
            {

                result4 = 0;
            }

            lblMediaEquipe.Text = result.ToString("0.##");
            lblMediaEquipeSus1.Text = result1.ToString("0.##");
            lblMediaEquipeSus2.Text = result2.ToString("0.##");
            lblMediaEquipeSus3.Text = result3.ToString("0.##");
            lblMediaEquipeSus4.Text = result4.ToString("0.##");


        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            PreencherTela();
        }

        protected void btnNovaPesquisa_Click(object sender, EventArgs e)
        {
            PnlConsulta.Visible = true;
            pnlResult.Visible = false;
            ddlUf.SelectedValue = "0";
            ddlMunicipio.SelectedValue = "0";

        }
    }
}